import logging

from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from services.chat_service import ChatService
from models.chat_schemas import ChatRequest, ChatHistoryResponse
from models.JsonData import JsonData
from agents.chat_agent import generate_stream_response
from core.auto import get_current_user
from core.exception import ApiException
from typing import Dict, Any


router = APIRouter(
    prefix="/api/chat",
    tags=["AI聊天助手"],
    dependencies=[Depends(get_current_user)],
)

@router.post("/stream")
async def chat_stream(request: ChatRequest,chat_service:ChatService = Depends(ChatService),
                      current_user: Dict[str, Any] = Depends(get_current_user)):

        account_no = current_user["accountNo"]
        print(f"用户{account_no}开始聊天")

        # 获取智能体
        result = StreamingResponse(
            generate_stream_response(chat_service,account_no,request.message),
            media_type="text/event-stream"
        )

        return result